using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AMStudio.Common;

namespace AMStudio.UserInterface
{
    public partial class UserAccountsForm : AWinFormShell
    {
        //private PassportAccount mypassports;
        public UserAccountsForm()
        {
            //this.mypassports = passports;
            InitializeComponent();
        }
        public void ConfigrationUI(string softwareUserName)
        {
            this.Text = softwareUserName + "-User Accounts";
        }
        private void LoginForm_Load(object sender, EventArgs e) 
        {
            UpdateList();
            if (this.listView1.Items.Count > 0)
                this.listView1.Items[0].Selected=true;
        }
        private void UpdateList()
        {
            this.listView1.Items.Clear();
            foreach (string k in UserDB.UserTags.Keys)
            {
                if (UserDB.UserTags[k].AuthorizationSet == 0)
                {
                    this.listView1.Items.Add(new ListViewItem(k, 1, this.listView1.Groups[0]));
                }
                else if (UserDB.UserTags[k].AuthorizationSet == 1)
                {
                    this.listView1.Items.Add(new ListViewItem(k, 0, this.listView1.Groups[1]));
                }
                else
                {
                    this.listView1.Items.Add(new ListViewItem(k, 0, this.listView1.Groups[2]));
                }
            }          
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (this.listView1.SelectedItems.Count > 0)
            //{
            //    this.groupPanel1.Text = "Password for " + this.listView1.SelectedItems[0].Text;
            //    this.labelX2.Text = "To change the password for " + this.listView1.SelectedItems[0].Text + ", click Reset Parssword";
            //}
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AddUsersForm temp = new AddUsersForm();
                if (temp.ShowDialog() == DialogResult.OK)
                {
                    User u = new User();
                    u.UserName = temp.UserName;
                    u.AuthorizationSet = temp.UserGroup;
                    u.Language = temp.Language;
                    UserDB.Add(u);
                    //this.mypassports.AddUser(temp.UserName, temp.UserGroup);
                    this.UpdateList();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
           
        }

        private void btnPassword_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                ChangePasswordsForm temp = new ChangePasswordsForm();
                if (temp.ShowDialog() == DialogResult.OK)
                {
                   string name=this.listView1.SelectedItems[0].Text;
                   UserDB.UserTags[name].Password = temp.Password;
                   UserDB.Update(UserDB.UserTags[name]);
                    //this.mypassports.ChangePassword(this.listView1.SelectedItems[0].Text, temp.Password);
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("You should select a user first");
            }          
        }
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                if (System.Windows.Forms.MessageBox.Show("Confirm to remove this account?","confirm",MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    UserDB.Delete(this.listView1.SelectedItems[0].Text);
                    this.UpdateList();
                }                              
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("You should select a user first");
            }          

        }    
        private void btnOK_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    this.mypassports.UpdatePassport();
            //    InformationForm.Show("Change accounts successfully");
            //}
            //catch (Exception ex)
            //{
            //    WrongForm.Show(ex.Message);
            //}       
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            
        }

        public override bool ActivateUI()
        {
            this.UIName = "UserAccountsForm";
            this.ShowDialog();
            return false;
        }
    }
}